import {
  findProfile,
  baseUserList,
  baseUserList2,
  baseUserAdd,
  baseUserEdit,
  fetchBaseUserBySid,
  unbind,
  fetchVehicleByUserAccountSid,
  findVin,
  bindVehicle,
  resetPassword,
  resetSecurityCode,
} from '../services/baseUserApi';

export default {
  namespace: 'baseUser',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    baseUserData: {
      list: [],
      pagination: {},
    },
    vindata: {},
    baseUserProfileData: {},
    loading: true,
  },
  effects: {
    *fetchBaseUserBySid({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchBaseUserBySid, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *findVin({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findVin, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchVehicleByUserAccountSid({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchVehicleByUserAccountSid, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetch({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(baseUserList2, payload);
      if (response.statusCode === '0') {
        yield put({
          type: 'baseUserSave',
          payload: response.data,
        });
        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback(response.data);
        return response.data;
      }
    },
    *findProfile({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findProfile, payload);
      yield put({
        type: 'baseUserProfile',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *resetPassword({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(resetPassword, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(baseUserAdd, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *edit({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(baseUserEdit, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *unbind({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(unbind, payload);
      yield put({
        type: 'vinsave',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *bindVehicle({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(bindVehicle, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },

    *resetSecurityCode({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(resetSecurityCode, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },

    *clearData({}, { put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      yield put({
        type: 'baseUserSave',
        payload: {
          list: [],
          pagination: {
            current: 1,
            total: 0,
          },
          rows: [],
        },
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    baseUserSave(state, action) {
      return {
        ...state,
        baseUserData: action.payload || {
          list: [],
          pagination: {
            current: 1,
            total: 0,
          },
          rows: [],
        },
      };
    },
    baseUserProfile(state, action) {
      return {
        ...state,
        baseUserProfileData: action.payload,
      };
    },
    vinsave(state, action) {
      return {
        ...state,
        vindata: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
